/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmModificarJornada.java
 *
 * Created on 19-may-2011, 8:43:13
 */

package ligafutbol;

import clases.*;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.table.*;

/**
 *
 * @author JAVA
 */
public class frmModificarJornada extends javax.swing.JFrame {

    Jornada jornada;
    ArrayList<Partido>lista;
    miModeloTabla mmt;

    /** Creates new form frmModificarJornada */
    public frmModificarJornada() {
        initComponents();
        jornada = new Jornada();
        jornada.jornadaActual();

        lista = Partido.partidosJornada(jornada);

        mmt = new miModeloTabla();
        mmt.rellenarDatos(lista);

        this.lbNum_Jornada.setText(""+jornada.getNumJornada());
        this.tbModResultadoPartido.setModel(mmt);
        this.tbModResultadoPartido.setRowHeight(20);



    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbTitulo = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbModResultadoPartido = new javax.swing.JTable();
        lbNum_Jornada = new javax.swing.JLabel();
        btnActualizar = new javax.swing.JButton();
        chkBFinalizada = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lbTitulo.setLabelFor(lbNum_Jornada);
        lbTitulo.setText("ACTUALIZAR JORNADA");

        tbModResultadoPartido.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tbModResultadoPartido);

        lbNum_Jornada.setBackground(new java.awt.Color(255, 255, 255));
        lbNum_Jornada.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbNum_Jornada.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbNum_Jornada.setOpaque(true);

        btnActualizar.setText("Actualizar");
        btnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarActionPerformed(evt);
            }
        });

        chkBFinalizada.setText("Finalizada");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(103, 103, 103)
                        .addComponent(lbTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbNum_Jornada, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(155, 155, 155)
                        .addComponent(btnActualizar)))
                .addContainerGap(15, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(287, Short.MAX_VALUE)
                .addComponent(chkBFinalizada, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(lbTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(lbNum_Jornada, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(chkBFinalizada)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addComponent(btnActualizar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarActionPerformed
        // TODO add your handling code here:
        miModeloTabla atm =  (miModeloTabla)this.tbModResultadoPartido.getModel();

        for(int i = 0; i < lista.size(); i++){
            for(int j = 0; j < atm.getColumnCount(); j++ ){
                Partido partido = (Partido)lista.get(i);
                if(j == 1){
                    if(atm.getValueAt(i, j) instanceof String){
                        partido.setGolesLocal(Integer.parseInt((String)atm.getValueAt(i, j)));
                    }
                    else{
                        partido.setGolesLocal((Integer)atm.getValueAt(i, j));
                    }
                }
                if(j == 2){
                    if(atm.getValueAt(i, j) instanceof String){
                        partido.setGolesVisitante(Integer.parseInt((String)atm.getValueAt(i, j)));
                    }
                    else{
                        partido.setGolesVisitante((Integer)atm.getValueAt(i, j));
                    }
                }
                lista.set(i, partido);
            }
        }

        if(this.chkBFinalizada.isSelected()){
            //Actualizar jornada
            jornada.setFinalizada(true);
        }
        
        Partido.actualizarPartidos(lista);
        this.setVisible(false);
    }//GEN-LAST:event_btnActualizarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void inicio() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmModificarJornada().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActualizar;
    private javax.swing.JCheckBox chkBFinalizada;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbNum_Jornada;
    private javax.swing.JLabel lbTitulo;
    private javax.swing.JTable tbModResultadoPartido;
    // End of variables declaration//GEN-END:variables


    class miModeloTabla extends javax.swing.table.AbstractTableModel{

        Object datos[][]= null;

        String columns[] = null;

        Class tipo [][] = null;

        Boolean editables[][] = null;

        public miModeloTabla() {

            columns = new String[]{"Equipo Local"," ", " ","Equipo Visitante"};
            datos = new Object[lista.size()][columns.length];
            tipo = new Class[][]{};
            editables = new Boolean[][]{};
        }




        private void rellenarDatos(ArrayList<Partido> lista){
            Iterator it = lista.iterator();

            Object d [][] = new Object[lista.size()][columns.length];
            Boolean edi [][] = new Boolean[lista.size()][columns.length];
            Class t [][] = new Class[lista.size()][columns.length];

            for(int i = 0; i< lista.size(); i++){
                Partido p = (Partido)lista.get(i);

                    d[i][0] = p.getEquipoLocal().getNombre();
                    edi[i][0] = false;
                    t[i][0] = java.lang.String.class;

                    d[i][1] = p.getGolesLocal();
                    edi[i][1] = true;
                    t[i][1] = java.lang.Integer.class;

                    d[i][2] = p.getGolesVisitante();
                    edi[i][2] = true;
                    t[i][2] = java.lang.Integer.class;

                    d[i][3] = p.getEquipoVisitante().getNombre();
                    edi[i][3] = false;
                    t[i][3] = java.lang.String.class;
            }
            tipo = t;
            editables = edi;
            datos = d;
        }

        public Object[][] getDatos(){
            return datos;
        }

        @Override
        public String getColumnName(int i){
            return columns[i];
        }

        @Override
        public int getRowCount() {
            return datos.length;
        }

        @Override
        public int getColumnCount() {
            return columns.length;
        }

        @Override
        public boolean isCellEditable(int i, int a){
            return editables[i][a];
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return datos[rowIndex][columnIndex];
        }

        @Override
        public void setValueAt(Object obj, int rowIndex, int columnIndex){
            datos[rowIndex][columnIndex] = obj;
        }

    }

}
